﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Ubiquity.Core.Extend;

namespace Ubiquity.Core.Web
{
    /// <summary>
    /// 树查询
    /// </summary>
    public static class TreeQuery
    {
        public static List<T> TreeWhere<T>(this List<T> entityList, Predicate<T> condition, string keyValue = "Id", string parentId = "ParentId") where T : class
        {
            List<T> locateList = entityList.FindAll(condition);
            var parameter = Expression.Parameter(typeof(T), "t");
            List<T> treeList = new List<T>();
            foreach (T entity in locateList)
            {
                treeList.Add(entity);
                var propertyInfo = entity.GetType().GetProperty(parentId);
                if (propertyInfo != null)
                {
                    string pId = propertyInfo.GetValue(entity, null).ToString();
                    while (true)
                    {
                        if (string.IsNullOrEmpty(pId) && pId == "0")
                        {
                            break;
                        }
                        Predicate<T> upLambda = (Expression.Equal(parameter.Property(keyValue), Expression.Constant(pId))).ToLambda<Predicate<T>>(parameter).Compile();
                        T upRecord = entityList.Find(upLambda);
                        if (upRecord != null)
                        {
                            treeList.Add(upRecord);
                            var memberInfo = upRecord.GetType().GetProperty(parentId);
                            if (memberInfo != null)
                                pId = memberInfo.GetValue(upRecord, null).ToString();
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
            return treeList.Distinct().ToList();
        }
    }
}
